// pages/indemnity/indemnity.js
const app = getApp();
var utils = require('../../utils/util.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		letter_addressList: [{
			value: '广东省内'
		},
		{
			value: '广东省外',
		},
		],
		letter_jiafangList: [{
			value: '财政国有'
		},
		{
			value: '民营企业',
		},
		],
		bankarrName:"请选择",
		companyarrName:"请选择",
		timearrName:"请选择",
		projectallName:"请选择",
		image_all:[]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			id:options.id
		})
	//	2. 履约保函     3.预付款保函     4.农民工保函
		if (app.globalData.type==2){
			wx.setNavigationBarTitle({
				title: '履约保函'
			})
		}
		if (app.globalData.type == 3) {
			wx.setNavigationBarTitle({
				title: '预付款保函'
			})
		}
		if (app.globalData.type == 4) {
			wx.setNavigationBarTitle({
				title: '农民工保函'
			})
		}
		this.getLetterDetail();
	},
	//获取保函详情
	getLetterDetail: function (e) {
		var that = this;
		utils.http("/api/v1/common/get_letter_detail", {
			letter_id: that.data.id
		}, function (res) {
			that.setData({
				detail: res.datas,
				bank_arr: res.datas.bank_arr, //出具银行
				company_arr: res.datas.company_arr, //企业资质
				time_arr: res.datas.time_arr,//担保期限
				project_arr: res.datas.project_arr,//项目类型
			})
		})
	},

	//出函银行
	bankArrChange:function(e){
		var that=this;
		that.setData({
			bankarrName:that.data.bank_arr[e.detail.value]
		})
	},
	//企业资质
	companyArrChange:function(e){
		var that = this;
		that.setData({
			companyarrName: that.data.company_arr[e.detail.value]
		})
	},
	//担保期限
	timeArrChange:function(e){
		var that = this;
		that.setData({
			timearrName: that.data.time_arr[e.detail.value]
		})
	},
	//项目类型
	projectArrChange:function(e){
		var that = this;
		that.setData({
			projectallName: that.data.project_arr[e.detail.value]
		})
	},
	//投标人所在地
	radioAddressChange:function(e){
		var that=this;
		console.log(e)
		that.setData({
			letter_address:e.detail.value
		})
	},
	//甲方性质
	radioJiaFangChange: function (e) {
		var that = this;
		console.log(e)
		that.setData({
			letter_jiafang: e.detail.value
		})
	},
	
	//上传文件
	uploadAction: function (e) {
		var that = this;
		wx.navigateTo({
			url: '../webviews/webviews',
			success: function (res) { },
			fail: function (res) { },
			complete: function (res) { },
		})
		// wx.chooseMessageFile({
		// 	count: 10,
		// 	type: 'file',
		// 	success(res) {
		// 		// tempFilePath可以作为img标签的src属性显示图片
		// 		const tempFiles = res.tempFiles
		// 		utils.showLoading("文件上传中");
		// 		wx.uploadFile({
		// 			url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
		// 			header: {
		// 				'Content-Type': 'multipart/form-data'
		// 			},
		// 			filePath: tempFiles[0].path,
		// 			name: 'file',
		// 			formData: {
		// 				key: app.globalData.key
		// 			},
		// 			success(result) {
		// 				utils.hideLoading();
		// 				var res = JSON.parse(result.data);
		// 				that.setData({
		// 					filePath: res.path,
		// 					tempFiles: tempFiles[0].name
		// 				})
		// 			},
		// 			fail: function (res) {
		// 				utils.hideLoading();
		// 				utils.message('上传服务器上传失败,请稍后再试2');
		// 				console.log(res);
		// 			}
		// 		})
		// 	}
		// })
	},
	//上传照片
	uploadImage: function () {
		var that = this;
		wx.chooseImage({
			success(res) {
				utils.showLoading("图片上传中");
				const tempFilePaths = res.tempFilePaths
				wx.uploadFile({
					url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
					header: {
						'Content-Type': 'multipart/form-data'
					},
					filePath: tempFilePaths[0],
					name: 'file',
					formData: {
						key: app.globalData.key
					},
					success: function (result) {
						utils.hideLoading();
						var image_all = that.data.image_all || [];
						//上传图片后返回的是字符串，需json.prise一下
						var res = JSON.parse(result.data);
						image_all.push({
							image: res.path,
							id: res.id
						})
						that.setData({
							image_all: image_all
						})
						console.log(that.data.image_all)
					},
					fail: function (res) {
						utils.hideLoading();
						utils.message('上传服务器上传失败,请稍后再试2');
						console.log(res);
					}
				})
			}
		})
	},
	//删除照片
	bindCloseTap: function (e) {
		var that = this;
		for (var i in that.data.image_all) {
			if (that.data.image_all[i].id == e.currentTarget.dataset.item) {
				that.data.image_all.splice(i, 1);
			}
		}

		that.setData({
			image_all: that.data.image_all
		})
		console.log(that.data.image_all)
	},

	//跳转确认订单
	formSubmit: function (e) {
		console.log(e);
		var that = this;
		var letter_price = e.detail.value.letter_price; //投保金额
		var letter_bank = that.data.bankarrName; //银行
		var letter_company = that.data.companyarrName; //企业选择
		var letter_date = that.data.timearrName;//担保期限
		var letter_project = that.data.projectallName;//项目类型
		var letter_phone = e.detail.value.letter_phone; //联系电话
		var letter_address = that.data.letter_address; //投标人所在地
		// var letter_files_images = that.data.image_all; //文件拍照列表
		var letter_files = app.globalData.docurl;
		var letter_jiafang = that.data.letter_jiafang; //甲方类型
		if (letter_price == "") {
			wx.showToast({
				title: '请输入投保金额',
				icon: "none"
			})
			return false;
		} 
		if (letter_bank == "请选择") {
			wx.showToast({
				title: '请选择出函银行',
				icon: "none"
			})
			return false;
		}
		if (letter_company == "请选择") {
			wx.showToast({
				title: '请选择企业资质',
				icon: "none"
			})
			return false;
		}
		if (letter_date == "请选择") {
			wx.showToast({
				title: '请选择担保期限',
				icon: "none"
			})
			return false;
		}
		if (letter_project == "请选择") {
			wx.showToast({
				title: '请选择项目类型',
				icon: "none"
			})
			return false;
		}
		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/; // 判断手机号码的正则
		if (!myreg.test(letter_phone)){
			wx.showToast({
				title: '错误的手机号码！',
				icon: "none"
			})
			return false;
		}
		if (letter_address==undefined){
			wx.showToast({
				title: '请选择投标人所在地',
				icon: "none"
			})
			return false;
		}
		if (letter_jiafang == undefined) {
			wx.showToast({
				title: '请选择甲方性质',
				icon: "none"
			})
			return false;
		}
		if (that.data.image_all.length <= 0) {
			wx.showToast({
				title: '请上传照片',
				icon: "none"
			})
			return false;
		}
		if (letter_files == undefined) {
			wx.showToast({
				title: '请上传文件',
				icon: "none"
			})
			return false;
		}
		var letter_files_images = [];
		for (var i in that.data.image_all) {
			letter_files_images.push(that.data.image_all[i].id)
		}
		wx.setStorageSync("detail", {
			letter_id: that.data.id,
			letter_price: letter_price,
			letter_date: letter_date,
			letter_bank: letter_bank,
			letter_company: letter_company,
			letter_address: letter_address,
			letter_files_images: letter_files_images,
			letter_files: app.globalData.docurl,
			letter_jiafang: letter_jiafang,
			letter_project: letter_project,
			letter_phone: letter_phone
		})
		wx.navigateTo({
			url: "../butorder/butorder",
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})